//
//  RightTabCellTwo.m
//  自定义collectionView
//
//  Created by apple on 15-1-7.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "RightTabCellTwo.h"

@implementation RightTabCellTwo
@synthesize cellFrameLayoutY;
@synthesize tabTwoBtn;
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andTitleArr:(NSArray *)arr andFrameBtnY:(float)btnY
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        itemArr  =[arr mutableCopy];
        MyLayout * flowLayout = [[MyLayout alloc] init];
        flowLayout.delegate = self;
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        
        
        tabCellCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 200, btnY) collectionViewLayout:flowLayout];
        [tabCellCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell0"];
        tabCellCollectionView.delegate = self;
        tabCellCollectionView.dataSource = self;
        tabCellCollectionView.backgroundColor = [UIColor whiteColor];
        tabCellCollectionView.contentSize =  CGSizeMake(200, 300);
        [self addSubview:tabCellCollectionView];
        
        tabTwoBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, btnY+5, 40, 15)];
        [self addSubview:tabTwoBtn];
        [tabTwoBtn setImage:[UIImage imageNamed:@"button_二级菜单收起"] forState:UIControlStateNormal];
    }
    return self;
}

//设置cell的高度
-(void)layoutSubviews
{
    [super layoutSubviews];
}

#pragma mark -  UICollectionViewDataSource && UICollectionViewDelegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return itemArr.count;
}

//每个UICollectionView展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"cell0";
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell sizeToFit];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:cell.bounds];
    titleLabel.text = itemArr[indexPath.row];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:11];
    titleLabel.layer.borderWidth = 1;
    titleLabel.layer.borderColor = [UIColor blueColor].CGColor;
    titleLabel.layer.cornerRadius = 5;
    [cell addSubview:titleLabel];
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *titleC = itemArr[indexPath.row];
    NSDictionary *attDic = @{NSFontAttributeName: [UIFont systemFontOfSize:11]};
    CGSize fontSize = [titleC sizeWithAttributes:attDic];
    return CGSizeMake(fontSize.width + 10, 20);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets top = {10,10,10, 10};//上 右
    return top;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%zd", indexPath.row);
}

#pragma  mark -  MyFlowLayoutDelegate
-(void)LastCollectionViewFlowLayoutFrameY:(float)orignY
{  //里面cell的高度不要超过tabCellCollectionView.frame 就不会滚动
    tabCellCollectionView.frame = CGRectMake(0, 0, 240, orignY+20+10);//本身的高度点+自身的高度+与底部间隔
    cellFrameLayoutY = orignY+20;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
- (void)collectionWithFrameYFromArray:(NSArray *)array
{
    
}
@end
